package com.greenline.search.report.model;

import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-4-16
 * Time: 下午2:41
 * To change this template use File | Settings | File Templates.
 */
public class GetNoResultsWord implements Serializable{
    private static Logger logger = LoggerFactory.getLogger(GetNoResultsWord.class);
    private List<SearchWord> currentWeekSearchWordList;
    private List<SearchWord> doctorNoResultsWord;
    private List<SearchWord> hospitalNoResultsWord;
    private MyHttpRequest httpRequest = new MyHttpClientRequest();

    public GetNoResultsWord(List<SearchWord> currentWeekSearchWordList){
        this.currentWeekSearchWordList = currentWeekSearchWordList;
        doctorNoResultsWord = new ArrayList<SearchWord>();
        hospitalNoResultsWord = new ArrayList<SearchWord>();
        init();
    }
    //判断一个词在医生和医院搜索中是否无结果，如果无结果则存储起来，
    //否则继续，最高是Configuration.TOP_NO_RESULT_WORD_COUNT个
    public void init(){
        String doctor_search = Configuration.DOCTOR_SEARCH + "rows=0&q=";
        String hospital_search = Configuration.HOSPITAL_SEARCH + "rows=0&q=";
        for(SearchWord searchWord : currentWeekSearchWordList){
            try {
                String encodeWord = URLEncoder.encode(searchWord.getText(),"UTF-8");
                String doctorSearchUrl = doctor_search + encodeWord;
                String hospitalSearchUrl = hospital_search + encodeWord;

                if(doctorNoResultsWord.size() < Configuration.TOP_NO_RESULT_WORD_COUNT){
                    ResponseInfo doctorResponse =
                            httpRequest.requestAndGetResponseInfo(doctorSearchUrl);
                    if(doctorResponse.getReturnCount() == 0){
                        doctorNoResultsWord.add(searchWord);
                    }
                }

                if(hospitalNoResultsWord.size() < Configuration.TOP_NO_RESULT_WORD_COUNT){
                    ResponseInfo hospitalResponse =
                            httpRequest.requestAndGetResponseInfo(hospitalSearchUrl);
                    if(hospitalResponse.getReturnCount() == 0){
                        hospitalNoResultsWord.add(searchWord);
                    }
                }


                logger.info(doctorSearchUrl);
                logger.info(hospitalSearchUrl);
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }
        }
    }

    public List<SearchWord> getCurrentWeekSearchWordList() {
        return currentWeekSearchWordList;
    }

    public void setCurrentWeekSearchWordList(List<SearchWord> currentWeekSearchWordList) {
        this.currentWeekSearchWordList = currentWeekSearchWordList;
    }

    public List<SearchWord> getDoctorNoResultsWord() {
        return doctorNoResultsWord;
    }

    public void setDoctorNoResultsWord(List<SearchWord> doctorNoResultsWord) {
        this.doctorNoResultsWord = doctorNoResultsWord;
    }

    public List<SearchWord> getHospitalNoResultsWord() {
        return hospitalNoResultsWord;
    }

    public void setHospitalNoResultsWord(List<SearchWord> hospitalNoResultsWord) {
        this.hospitalNoResultsWord = hospitalNoResultsWord;
    }
}
